﻿function GeneratePieChart(chartContainer, chartTitle, chartTotal, chartSeries, chartData, selectedPointField) {
    var options = {
        chart: {
            renderTo: chartContainer,
            backgroundColor: 'transparent',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        credits: {
            enabled: false
        },
        title: {
            text: chartTitle,
            style: {
                color: '#000000',
                fontSize: '18px',
                fontFamily: 'Morpheus'
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>' + this.point.name + '</b>: ' + RoundNumber(((this.y / chartTotal) * 100), 2) + '%';
            }
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>' + this.point.name + '</b>: ' + this.y;
                    }
                }
            },
            series: {
                events: {
                    click: function(event) {
                        if (event.point.selected != true) {
                            $('#' + selectedPointField).val(event.point.name);
                            $('#' + selectedPointField).trigger('change');
                        } else {
                            $('#' + selectedPointField).val('');
                            $('#' + selectedPointField).trigger('change');
                        }
                    }
                }
            }
        },
        series: []
    };

    var series = {
        type: 'pie',
        name: chartSeries,
        data: []
    };
    var data = chartData.split(";");
    for (key in data) {
        var single = data[key].split(",");
        var tmp = new Array(single[0], single[1] * 1);
        series.data.push(tmp);
    }
    options.series.push(series);

    new Highcharts.Chart(options);
}